package com.cy.dda.web.front.collection;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.cy.dda.comm.utils.CommonUtils;
import com.cy.dda.model.PageBean;
import com.cy.dda.model.Result;
import com.cy.dda.model.ao.collection.CollectionRecordAddAO;
import com.cy.dda.model.query.collection.CollectionRecordListQuery;
import com.cy.dda.model.vo.collection.CollectionRecordAllListVO;
import com.cy.dda.model.vo.collection.CollectionRecordDetailInfoVO;
import com.cy.dda.model.vo.collection.CollectionRecordListVO;
import com.cy.dda.model.vo.statistics.StatisticsCollectionRecordVO;
import com.cy.dda.service.collection.CollectionRecordService;
import com.cy.dda.web.BaseController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

/**
 * 前台-催收记录
 *
 * @author: tanyilin
 * @create: 2018年11月2日 下午4:48:56
 */
@Slf4j
@RestController
@RequestMapping("/user/collection/record")
@Api(tags = "前台-催收记录")
public class CollectionRecordController extends BaseController {

	@Autowired
	private CollectionRecordService collectionRecordService;
	
	@ApiOperation("新增催收记录")
	@PostMapping("/do/add")
	public Result doAdd(@Validated @RequestBody CollectionRecordAddAO addAO, BindingResult bindingResult) {
		this.checkError(bindingResult);
		if(CommonUtils.isEmpty(addAO.getAddress()) || "无".equals(addAO.getAddress())) {
			state(false, "请点击获取您当前的位置");
		}
		collectionRecordService.addCollectionRecord(addAO, this.getUser().getUserId());
		return ok();
	}
	
	@ApiOperation("催收记录（日志）列表")
	@GetMapping("/list")
	public PageBean<CollectionRecordListVO> list(CollectionRecordListQuery query) {
		PageBean<CollectionRecordListVO> pageBean = collectionRecordService.selectCollectionRecordList(query);
		return pageBean;
	}
	
	@ApiOperation("近七天催收日志数量统计")
	@GetMapping("/statistics/count")
	public Result<List<StatisticsCollectionRecordVO>> statisticsCount() {
		Long userId = this.getUser().getUserId();
		List<StatisticsCollectionRecordVO> list = collectionRecordService.selectStatisticsCollectionRecordVO(userId, 7);
		return ok("", list);
	}
	
	@ApiOperation("（催收员用户）我的催收记录（日志）列表")
	@GetMapping("/my/list")
	public PageBean<CollectionRecordListVO> myList(CollectionRecordListQuery query) {
		Long userId = this.getUser().getUserId();
		query.setUserId(userId);
		query.setCasesId(null);
		query.setCustomerId(null);
		if(CommonUtils.isEmpty(query.getCreateDate())) {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			query.setCreateDate(sdf.format(new Date()));
		}
		PageBean<CollectionRecordListVO> pageBean = collectionRecordService.selectCollectionRecordList(query);
		return pageBean;
	}
	
	@ApiOperation("（催收员用户）我的全部催收记录（日志）列表")
	@GetMapping("/my/all/list")
	public PageBean<CollectionRecordAllListVO> myAllList(CollectionRecordListQuery query) {
		Long userId = this.getUser().getUserId();
		query.setUserId(userId);
		query.setCasesId(null);
		query.setCustomerId(null);
		PageBean<CollectionRecordAllListVO> pageBean = collectionRecordService.selectCollectionRecordAllList(query);
		return pageBean;
	}
	
	@ApiOperation("前台催收记录（日志）详情")
	@GetMapping("/get/detail/info")
	public Result<CollectionRecordDetailInfoVO> getDetailInfo(@RequestParam Long collectionRecordId) {
		CollectionRecordDetailInfoVO collectionRecordDetailInfoVO = collectionRecordService.selectCollectionRecordDetailInfoVO(collectionRecordId);
		return ok("", collectionRecordDetailInfoVO);
	}
	
}
